package com.jrx.anytxn.param.service.partner.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.partner.PmMerchantTableReq;
import com.jrx.anytxn.param.dto.partner.PmMerchantTableRes;
import com.jrx.anytxn.param.entity.PmMerchantTable;
import com.jrx.anytxn.param.mapper.ext.ExtPmMerchantTableMapper;
import com.jrx.anytxn.param.service.partner.IPmMerchantTableService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户管理service实现
 *
 * @author : zhenghaopu
 * 2020/2/24
 */
@CacheConfig(cacheNames = "MERCHANT")
@Service
public class PmMerchantTableServiceImpl implements IPmMerchantTableService {

    private static final Logger logger = LoggerFactory.getLogger(PmMerchantTableServiceImpl.class);

    @Resource
    private ExtPmMerchantTableMapper extPmMerchantTableMapper;
    
    
    @Autowired
    private ISegmentService segmentService;


    @Override
    public PmMerchantTableRes getByTenIdIdAndMerchanId(String tenantId, String merchantId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(merchantId)) {
            logger.error("商户ID或租户ID为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PmMerchantTable pmMerchantTable = extPmMerchantTableMapper.selectByTenantIdAndMerchantId(tenantId,merchantId);
        if (pmMerchantTable == null) {
            logger.error("未查到商户数据");
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(pmMerchantTable, PmMerchantTableRes.class);
    }

    @Override
    public PmMerchantTableRes addMerchantTable(PmMerchantTableReq pmMerchantTableReq) throws TxnBizException {
        PmMerchantTable table = extPmMerchantTableMapper.selectByMerchantIdAndOrgIdAndTenantId(pmMerchantTableReq.getMerchantId(),
                pmMerchantTableReq.getOrganizationId(),pmMerchantTableReq.getTenantId());
        if (table != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PmMerchantTable pmMerchantTable = BeanMapping.copy(pmMerchantTableReq, PmMerchantTable.class);
        try {
            pmMerchantTable.setId(segmentService.getId(Constant.PM_MERCHANT_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        pmMerchantTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPmMerchantTableMapper.insertSelective(pmMerchantTable);
        return BeanMapping.copy(pmMerchantTableReq, PmMerchantTableRes.class);
    }

    @Override
    public TxnPage<PmMerchantTableRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnBizException {
        Page<PmMerchantTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PmMerchantTable> tableList = extPmMerchantTableMapper.selectByTenantId(tenantId);
        if (CollectionUtils.isEmpty(tableList)) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PmMerchantTableRes> res = BeanMapping.copyList(tableList, PmMerchantTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @CacheEvict(key="'merchant_'+#pmMerchantTableReq.tenantId+'_'+#pmMerchantTableReq.merchantId")
    @Override
    public PmMerchantTableRes modifyPmMerchantTable(PmMerchantTableReq pmMerchantTableReq) throws TxnBizException {
        PmMerchantTable table = extPmMerchantTableMapper.selectByTenantIdAndMerchantId(pmMerchantTableReq.getTenantId(),
                pmMerchantTableReq.getMerchantId());
        if (table == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PmMerchantTable pmMerchantTable = BeanMapping.copy(pmMerchantTableReq, PmMerchantTable.class);
        // 更新
        extPmMerchantTableMapper.updateByTenantIdAndMerchantId(pmMerchantTable);
        return BeanMapping.copy(pmMerchantTableReq, PmMerchantTableRes.class);
    }

    @CacheEvict(key="'merchant_'+#tenantId+'_'+#merchantId")
    @Override
    public Boolean removeByTenantIdAndMerchantId(String tenantId, String merchantId) throws TxnBizException {
        if (org.apache.commons.lang.StringUtils.isBlank(tenantId) || org.apache.commons.lang.StringUtils.isBlank(merchantId)) {
            logger.error("租户id或商户id不可为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPmMerchantTableMapper.deleteByTenantIdAndMerchantId(tenantId, merchantId);
        return i > 0;
    }

    @Cacheable(key="'merchant_'+#tenantId+'_'+#merchantId")
    @Override
    public PmMerchantTable getByMerchanId(String tenantId, String merchantId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(merchantId)) {
            logger.error("商户ID或租户ID为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PmMerchantTable pmMerchantTable = extPmMerchantTableMapper.selectByTenantIdAndMerchantId(tenantId,merchantId);
        if(pmMerchantTable == null){
            logger.error("查询商户信息为空");
            throw new TxnBizException(TxnRespCode.MERCHANT_NOT_EXIT.getCode(),TxnRespCode.MERCHANT_NOT_EXIT.getUserTips());
        }
        return pmMerchantTable;
    }

    @Override
    public List<PmMerchantTableRes> findAllByTenantId(String tenantId) throws TxnException {
        if (StringUtils.isBlank(tenantId)) {
            logger.error("租客号为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY);
        }
        List<PmMerchantTable> pmMerchantTableResList = extPmMerchantTableMapper.selectByTenantId(tenantId);
        return BeanMapping.copyList(pmMerchantTableResList, PmMerchantTableRes.class);
    }

}
